﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using NursingFormSystem;

namespace NursingFormSystem
{
    /// <summary>
    /// 体温单打印文档
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(false)]
    public class TemperaturePrintDocument : PrintDocument 
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="document">文档对象</param>
        /// <param name="render">绘制器</param>
        public TemperaturePrintDocument(
            TemperatureDocument document,
            TemperatureDocumentRender render)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }
            if( render == null )
            {
                throw new ArgumentNullException("render");
            }
            _Document = document;
            _Render = render;
        }
       
        /// <summary>
        /// 文档对象
        /// </summary>
        private TemperatureDocument _Document = null;
        /// <summary>
        /// 文档视图绘制器
        /// </summary>
        private TemperatureDocumentRender _Render = null;

        /// <summary>
        /// 当前页码号
        /// </summary>
        private int _CurrentPageIndex = 0 ;

        private int _SpecifyPageIndex = -1;
        /// <summary>
        /// 打印指定的页码
        /// </summary>
        [System.ComponentModel.DefaultValue( -1 )]
        public int SpecifyPageIndex
        {
            get
            {
                return _SpecifyPageIndex; 
            }
            set
            {
                _SpecifyPageIndex = value; 
            }
        }

        private int _FromPage = 1;
        /// <summary>
        /// 打印指定的开始页码
        /// </summary>
        [System.ComponentModel.DefaultValue(1)]
        public int FromPage
        {
            get
            {
                return _FromPage;
            }
            set
            {
                _FromPage = value;
            }
        }

        private int _ToPage = 1;
        /// <summary>
        /// 打印指定的结束页码
        /// </summary>
        [System.ComponentModel.DefaultValue(1)]
        public int ToPage
        {
            get
            {
                return _ToPage;
            }
            set
            {
                _ToPage = value;
            }
        }

        /// <summary>
        /// 开始打印任务
        /// </summary>
        /// <param name="e"></param>
        protected override void OnBeginPrint(PrintEventArgs e)
        {
            DateTime maxDate = TemperatureDocument.NullDate;
            DateTime minDate = TemperatureDocument.NullDate;
            _CurrentPageIndex = 0;
            _Document.UpdateState();
            _Document.UpdateNumOfPage(out maxDate, out minDate);
            base.OnBeginPrint(e);
        }

        bool Range = false;
        /// <summary>
        /// 打印一页
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            base.OnPrintPage(e);
            // 设置当前页码号
            if (_SpecifyPageIndex >= 0)
            {
                _CurrentPageIndex = _SpecifyPageIndex;
            }
            if (Range != true && FromPage - 1 >= 0 && _SpecifyPageIndex < 0)
            {
                Range = true;
                _CurrentPageIndex = FromPage-1;
            }
            _Document.PageIndex = _CurrentPageIndex;
            // 设置计量单位
            e.Graphics.PageUnit = GraphicsUnit.Document;
            // 设置文档位置
            _Document.Options.Left = e.PageSettings.Margins.Left * 3;
            _Document.Options.Top = e.PageSettings.Margins.Top * 3;
            _Document.Options.Width = e.PageSettings.Bounds.Width * 3 - _Document.Options.Left - e.PageSettings.Margins.Right * 3;
            _Document.Options.Height = e.PageSettings.Bounds.Height * 3 - _Document.Options.Top - e.PageSettings.Margins.Bottom * 3;
            //_Document.Options.Left = e.PageSettings.Margins.Left;
            //_Document.Options.Top = e.PageSettings.Margins.Top;
            //_Document.Options.Width = e.PageSettings.Bounds.Width - _Document.Options.Left - e.PageSettings.Margins.Right;
            //_Document.Options.Height = e.PageSettings.Bounds.Height - _Document.Options.Top - e.PageSettings.Margins.Bottom;
            // 绘制文档
            _Render.Draw(e.Graphics, _Document,1);
            _CurrentPageIndex++;
            if ( _SpecifyPageIndex >= 0 || _CurrentPageIndex >= _Document.NumOfPages)
            {
                // 若超过最大页则没有后续页，不再打印下一页
                e.HasMorePages = false;
            }
            else if (_CurrentPageIndex >= ToPage)
            {
                e.HasMorePages = false;
            }
            else
            {
                e.HasMorePages = true;
            }
        }
    }
}
